package com.ecoolex.ying.space.api.graphql;

import java.util.Date;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.entity.UserWorks;
import com.ecoolex.ying.space.entity.UserWorksComment;
import com.ecoolex.ying.space.enums.UserLikeRecordType;
import com.ecoolex.ying.space.pojo.AddCommentDTO;
import com.ecoolex.ying.space.pojo.AddReplyDTO;
import com.ecoolex.ying.space.ws.UserLikeRecordService;
import com.ecoolex.ying.space.ws.UserWorksCommentService;
import com.ecoolex.ying.space.ws.UserWorksService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;


@GraphQL2Module(type = Type.User, description = "用户作品、灵感评论")
public class UserWorksCommentGraphql {

    @Reference
    private UserWorksCommentService userWorksCommentService;

    @Autowired
    private ConsumerMemberManager consumerMemberManager;

    @Reference
    private UserWorksService userWorksService;

    @Reference
    private UserLikeRecordService userLikeRecordService;

    @TypeField(type = UserWorksComment.class, field = "commentUser", description = "评论用户")
    public ConsumerMember getCommentUser(@Source UserWorksComment source) {
        return consumerMemberManager.getByUserId(source.getCommentUserId());
    }

    @TypeField(type = UserWorksComment.class, field = "beReplyUser", description = "被回复用户")
    public ConsumerMember getBeCommentUser(@Source UserWorksComment source) {
        return consumerMemberManager.getByUserId(source.getBeCommentUserId());
    }

    @TypeField(type = UserWorksComment.class, field = "replyComment", description = "被回复的评论")
    public UserWorksComment replyComment(@Source UserWorksComment source) {
        return userWorksCommentService.getByIdForH5(source.getReplyId());
    }

    @TypeField(type = UserWorksComment.class, field = "ifPrase", description = "是否点赞了")
    public boolean ifPrase(@Source UserWorksComment source, ConsumerMemberAuthority user) {
        if (user.isLogin()) {
            return userLikeRecordService.getByUserIdAndTypeAndContentId(user.userId(),
                UserLikeRecordType.COMMENT.real(),
                source.getCommentId()) == null ? false
                    : true;
        }
        return false;
    }

    @TypeField(type = UserWorksComment.class, field = "replyList", description = "回复评论列表")
    public Pagination<UserWorksComment> replyList(@Source UserWorksComment source, @NonNull Date now, @NonNull Sorting[] sorts, @NonNull Page page) {
        return userWorksCommentService.queryReplyForH5(source.getCommentId(), now, sorts, page);
    }

    @QueryField(field = "userWorksComment", description = "评论详情")
    public UserWorksComment getById(@NonNull Integer commentId) {
        return userWorksCommentService.getByIdForH5(commentId);
    }

    @QueryField(field = "userWorksCommentList", description = "作品评论列表")
    public Pagination<UserWorksComment> query(@NonNull Integer worksId, @NonNull Date now, @NonNull Sorting[] sorts, @NonNull Page page) {

        return userWorksCommentService.queryCommentForH5(now, worksId, sorts, page);
    }

    @QueryField(field = "replyUserWorksCommentList", description = "评论回复列表")
    public Pagination<UserWorksComment> queryreplyList(@NonNull Integer lastCommentId, @NonNull Date now, @NonNull Sorting[] sorts, @NonNull Page page) {
        return userWorksCommentService.queryReplyForH5(lastCommentId, now, sorts, page);
    }

    @QueryField(field = "sendCommentList", description = "我发出的评论列表")
    public Pagination<UserWorksComment> querySendComment(@NonNull Sorting[] sorts, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return userWorksCommentService.queryByCommentUserIdForH5(user.userId(), sorts, page);
    }

    @QueryField(field = "receivedCommentList", description = "我收到的评论列表")
    public Pagination<UserWorksComment> queryReceivedComment(@NonNull Date now, @NonNull Sorting[] sorts, @NonNull Page page, ConsumerMemberAuthority user) {
        user.assertLogin();
        return userWorksCommentService.queryByBeCommentUserIdForH5(user.userId(), now, sorts, page);
    }

    @QueryField(field = "unreadReceivedCommentCount", description = "我收到的评论未读数量")
    public long unreadReceivedCommentCount(ConsumerMemberAuthority user) {
        if (user.isLogin()) {
            return userWorksCommentService.unreadReceivedCommentCount(user.userId());
        } else {
            return 0;
        }
    }

    @MutationField(field = "addUserWorksComment", description = "添加内容评论")
    public UserWorksComment comment(@NonNull AddCommentDTO record, ConsumerMemberAuthority user) {
        user.assertLogin();
        // 检查被评论的内容是否存在
        UserWorks userWorks = userWorksService.getUserWorksById(record.getWorksId());
        Assert.isTrue(Check.notNull(userWorks), ResultCode.VERIFY.build(1), "作品不存在");
        record.setCommentUserId(user.userId());
        record.setWorksUserId(userWorks.getUserId());
        return userWorksCommentService.addUserWorksComment(record, user.userName());
    }

    @MutationField(field = "addUserWorksReply", description = "添加评论回复")
    public UserWorksComment comment(@NonNull AddReplyDTO record, ConsumerMemberAuthority user) {
        user.assertLogin();
        // 检查传入的上级被评论id是否存在
        UserWorksComment worksComment = userWorksCommentService.getByIdForH5(record.getLastCommentId());
        Assert.isTrue(Check.notNull(worksComment), ResultCode.VERIFY.build(1), "评论不存在");
        record.setCommentUserId(user.userId());
        record.setWorksUserId(worksComment.getWorksUserId());
        record.setWorksId(worksComment.getWorksId());
        return userWorksCommentService.addUserWorksReply(record, user.userName());
    }

    @MutationField(field = "delUserWorksComment", description = "删除我的评论")
    public boolean delete(@NonNull Integer id, ConsumerMemberAuthority user) {
        user.assertLogin();
        UserWorksComment userWorksComment = userWorksCommentService.getByIdForH5(id);
        Assert.isTrue(Check.notNull(userWorksComment), ResultCode.VERIFY.build(1), "评论不存在");
        Assert.isTrue(user.userId() == userWorksComment.getCommentUserId(), ResultCode.VERIFY.build(2), "无法删除别人的评论");
        userWorksCommentService.delete(userWorksComment, user.userName());
        return true;
    }

    @MutationField(field = "updateUserWorksCommentReadState", description = "消息中心-更新收到的评论读取状态")
    public boolean updateUserWorksCommentReadState(ConsumerMemberAuthority user) {
        user.assertLogin();
        userWorksCommentService.updateUserWorksCommentReadState(user.userId(), user.userName());
        return true;
    }
}
